package com.zjhcsoft.shop.action;

import java.util.Arrays;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.json.JSONArray;
import org.springframework.stereotype.Controller;

import com.zjhcsoft.common.action.SysAbstractAction;
import com.zjhcsoft.shop.model.History;
import com.zjhcsoft.shop.model.Product;
import com.zjhcsoft.shop.model.Serial;
import com.zjhcsoft.shop.model.Type;
import com.zjhcsoft.shop.model.User;
import com.zjhcsoft.shop.service.HistoryService;
import com.zjhcsoft.shop.service.ProductService;
import com.zjhcsoft.shop.sys.buffer.TypeBuffer;
import com.zjhcsoft.shop.sys.shoputil.UserUtil;
import com.zjhcsoft.util.CommonUtil;

@Controller
public class ProductAction extends SysAbstractAction {
	private static final long serialVersionUID = -3835362354761255643L;
	@Resource
	private ProductService productService;
	@Resource
	private HistoryService historyService;
	
	Log logger = LogFactory.getLog(ProductAction.class);
	
	private Long id;
	private Product product;
	private List<String> smallPics;
	private List<String> bigPics;
	private List<String> detailPics;
	private List<History> historys;
	private List<Type> types;
	private List<Serial> serials;
	private User user;

	


	public String show(){
		if(id!=null&&!"".equals(id)){
			product=productService.load(id);
			String productDesc=product.getProductDesc();
			if(productDesc!=null){
				product.setProductDesc(productDesc.replaceAll("\n", "<br/>"));
			}
			smallPics=CommonUtil.jsonStr2List(product.getSmallpic());
			bigPics=CommonUtil.jsonStr2List(product.getBigpic());
			detailPics=CommonUtil.jsonStr2List(product.getDetailpic());
			//登录用户
			user=UserUtil.getUserFromCookies();
			//获取最近浏览记录
			historys=historyService.getHistoryList(product);
		}
		return "success";
	}

	//新增页面
	public String add(){
		product=null;
		types=TypeBuffer.CLOTHES_TYPE_LIST_FROMDB;
		return "success";
	} 
	//保存产品信息
	public String save(){
		try {
			if(product!=null){
				productService.saveOrUpdate(product);
			}
		} catch (Exception e) {
			logger.error(e.getMessage());
			return "error";
		}
		return "success";
	}
	
	//根据类型获取系列
	public String getSonSerials(){
		serials=TypeBuffer.CLOTHES_SERIAL_LIST_FROMDB;
		return "success";
	}
	
	public Product getProduct() {
		return product;
	}


	public void setProduct(Product product) {
		this.product = product;
	}


	public Long getId() {
		return id;
	}


	public void setId(Long id) {
		this.id = id;
	}


	public List<String> getSmallPics() {
		return smallPics;
	}


	public void setSmallPics(List<String> smallPics) {
		this.smallPics = smallPics;
	}


	public List<String> getBigPics() {
		return bigPics;
	}


	public void setBigPics(List<String> bigPics) {
		this.bigPics = bigPics;
	}


	public List<String> getDetailPics() {
		return detailPics;
	}


	public void setDetailPics(List<String> detailPics) {
		this.detailPics = detailPics;
	}

	public List<History> getHistorys() {
		return historys;
	}


	public void setHistorys(List<History> historys) {
		this.historys = historys;
	}



	public User getUser() {
		return user;
	}


	public void setUser(User user) {
		this.user = user;
	}

	public List<Type> getTypes() {
		return types;
	}

	public void setTypes(List<Type> types) {
		this.types = types;
	}

	public List<Serial> getSerials() {
		return serials;
	}

	public void setSerials(List<Serial> serials) {
		this.serials = serials;
	}


	
	
}
